﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace WCFServiceWebRole1
{
    public class UsersDataSource
    {
        private UsersDataServiceContext _ServiceContext = null;

        public UsersDataSource(CloudStorageAccount storageAccount)
        {
            // var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            _ServiceContext = new UsersDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);

            // Create the tables
            // In this case, just a single table.  
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(UsersDataServiceContext.UsersTableName);
        }

        public IEnumerable<UsersDataModel> Select()
        {
            var results = (from c in _ServiceContext.UsersTable
                           select c).Take(100);

            var query = results.AsTableServiceQuery<UsersDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
        public IEnumerable<UsersDataModel> SelectAll()
        {
            var results = from c in _ServiceContext.UsersTable
                           select c;

            var query = results.AsTableServiceQuery<UsersDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
        public IEnumerable<UsersDataModel> Select(string name)
        {
            var results = from c in _ServiceContext.UsersTable
                          where c.Username==name
                           select c;

            var query = results.AsTableServiceQuery<UsersDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
     
        public void Update(string username, string Ip, int port, string status)
        {

            var results = (from c in _ServiceContext.UsersTable
                          where c.Username== username
                           select c).Take(1);

            var query = results.AsTableServiceQuery<UsersDataModel>();
            UsersDataModel user = query.Execute().First();
            user.IP = Ip;
            user.Port = port;
            user.Status = status;

            _ServiceContext.UpdateObject(user);
            _ServiceContext.SaveChanges();
        }

        public bool Login(string username, string password)
        {
            var results = (from c in _ServiceContext.UsersTable
                           where c.Username == username
                           select c).Take(1);

            var query = results.AsTableServiceQuery<UsersDataModel>();
            UsersDataModel user = query.Execute().First();

            return string.Equals(user.Password, password);
        }


        public void Add(UsersDataModel usersDataModel)
        {
            _ServiceContext.UpdateObject(usersDataModel);
            _ServiceContext.SaveChanges();
        }

        public void Edit(StoreRecord user)
        {
            var results = from c in _ServiceContext.UsersTable
                          where c.Username==user.Username
                           select c;

            var query = results.AsTableServiceQuery<UsersDataModel>();
            var queryResults = query.Execute().First();

            queryResults.Username = user.Username;
            queryResults.Status = user.Status;
            queryResults.Port = user.Port;
            queryResults.Password = user.Password;
            queryResults.IP = user.Ip;
            
            _ServiceContext.UpdateObject(queryResults);
            _ServiceContext.SaveChanges();
        }

        public void Delete(string user)
        {
            var results = from c in _ServiceContext.UsersTable
                          where c.Username == user
                          select c;

            var query = results.AsTableServiceQuery<UsersDataModel>();
            var queryResults = query.Execute().First();

            _ServiceContext.DeleteObject(queryResults);
            _ServiceContext.SaveChanges();
        }
    }
}
